package com.loooooo.pm2024.modules.res.service;

import com.loooooo.pm2024.modules.res.mapper.ResourceFileMapper;
import com.loooooo.pm2024.modules.res.model.ResourceFile;
import com.loooooo.pm2024.modules.res.selector.FileInfoSelector;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import com.loooooo.pm2024.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
public class ResourceFileService {
    @Autowired
    ResourceFileMapper resourceFileMapper;
    @Autowired
    IdService idService;
    @Autowired
    TransactionTemplate transactionTemplate;

    public ResourceFile get(Long id) {
        return resourceFileMapper.get(id);
    }

    public List<ResourceFile> list(FileInfoSelector selector) {
        return resourceFileMapper.select(selector);
    }

    public Integer count(FileInfoSelector selector) {
        return resourceFileMapper.count(selector);
    }

    public Integer update(ResourceFile obj, MultipartFile multipartFile) {
        long now = System.currentTimeMillis();
        Long userId = SecurityUtils.loginedUserId();
        obj.setUpdatedAt(now);
        obj.setUpdatedBy(userId);

        System.out.println("upload15 ...");

        int r = transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus status) {
                try {
                    int r = -1;
                    if (multipartFile != null && !multipartFile.isEmpty()) {
                        obj.setSize((int) multipartFile.getSize());
                        obj.setContentType(multipartFile.getContentType());
                    }
                    if (!StringUtils.hasText(obj.getFilename())) {
                        if (multipartFile != null) {
                            obj.setFilename(multipartFile.getOriginalFilename());
                        }
                    }
                    if (!obj.idAvailable() || obj.isNewEntity()) {
                        if (!obj.idAvailable()) obj.setId(idService.nextId());
                        obj.setCreatedAt(now);
                        obj.setCreatedBy(userId);
                        r = resourceFileMapper.insert(obj);
                    } else {
                        r = resourceFileMapper.update(obj);
                    }
                    if (multipartFile != null && !multipartFile.isEmpty()) {
                        File dir = new File("/image2/pm2024/res/", obj.getPath());
                        File file = new File(dir.getAbsolutePath(), obj.getFilename());
                        IOUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);

                    }
                    return r;
                } catch (Exception ex) {

                    status.setRollbackOnly();
                    return -1;
                }
            }
        });


        return r;
    }

    public Integer delete(Long id) {
        return 0;
    }
}
